package com.oss;


/**

 * Copyright (c) 2013-2016, Jieven. All rights reserved.
 *
 * Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt
 * To use it on other terms please contact us at 1623736450@qq.com
 */

import java.util.HashMap;
import java.util.HashSet;
import com.eova.common.utils.xx;
import com.eova.config.EovaConfig;
import com.eova.interceptor.LoginInterceptor;
import com.eova.user.UserController;
import com.jfinal.config.Interceptors;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.template.Engine;
import com.oss.product.ProductController;
import com.wx.controller.K2ApiController;
import com.wx.controller.MarketingController;
import com.wx.controller.OrderController;
import com.wx.controller.OrderController22;
import com.wx.controller.ShopOrderController;
import com.wx.controller.UserIndexController;
import com.wx.controller.WeiXinOauthController;
import com.wx.controller.XbOrderController;
import com.wx.model.PartyGetBonusRecord;
import com.wx.model.PartyInfo;
import com.wx.model.PartyLotteryNum;
import com.wx.model.PartyPrizeDetail;
import com.wx.model.PartyPrizeGroup;
import com.wx.model.PartyPrizeRecord;
import com.wx.model.PartyPrizeSummary;
import com.wx.model.PartyRule;
import com.wx.model.PartyTicket;
import com.wx.model.PartyTicketDetail;
import com.wx.model.PartyType;
import com.wx.model.PartyUser;
import com.wx.model.PartyUserSerinfo;

public class OSSConfig extends EovaConfig {
	
	
	
	/**
     * 配置模板引擎，通常情况只需配置共享的模板函数
     */
    @Override
	public void configEngine(Engine me) {
    	super.configEngine(me);
    	me.addSharedFunction("/wx/common/__layjss.html");
    }
	/**
	 * 系统启动之后
	 */
	@Override
	public void afterJFinalStart() {
		super.afterJFinalStart();
		loadProp("default/wx.config", "default/wx.config");
	}
	
	public void loadProp(String pro, String dev) {
		try {
//			List<WxAccount> accountList = AccountService.me.getAllAccountList();
//			if(accountList.size()==0){
//				PropKit.use(pro);
//				ApiConfig ac = new ApiConfig();
//				// 配置微信 API 相关参数
//				ac.setToken(PropKit.get("token"));
//				ac.setAppId(PropKit.get("appId"));
//				ac.setAppSecret(PropKit.get("appSecret"));
//
//				/**
//				 *  是否对消息进行加密，对应于微信平台的消息加解密方式：
//				 *  1：true进行加密且必须配置 encodingAesKey
//				 *  2：false采用明文模式，同时也支持混合模式
//				 */
//				ac.setEncryptMessage(PropKit.getBoolean("encryptMessage", false));
//				ac.setEncodingAesKey(PropKit.get("encodingAesKey", "setting it in config file"));
//
//				/**
//				 * 多个公众号时，重复调用ApiConfigKit.putApiConfig(ac)依次添加即可，第一个添加的是默认。
//				 */
//				ApiConfigKit.putApiConfig(ac);
//				
//			}else{
//				for(WxAccount wx :accountList){
//					ApiConfig ac = new ApiConfig();
//					ac.setToken(wx.getToken());
//					ac.setAppId(wx.getAppid());
//					ac.setAppSecret(wx.getAppsecret());
//					if(wx.getEncryptmessage() == 0 ){
//						ac.setEncryptMessage(false);
//					}else{
//						ac.setEncryptMessage(true);
//					}
//					if(StrUtil.isBlank(wx.getEncodingseskey())){
//						ac.setEncodingAesKey("NULL");
//					}else{
//						ac.setEncodingAesKey(wx.getEncodingseskey());
//					}
//					
//					ApiConfigKit.putApiConfig(ac);
//				}
//			}
//			ApiConfigKit.setAccessTokenCache(new RedisAccessTokenCache());
			
		}
		catch (Exception e) {
			e.printStackTrace();
			PropKit.use(dev);
		} 
	}
	/**
	 * 自定义路由
	 *
	 * @param me
	 */
	@Override
	protected void route(Routes me) {
		
		// 自定义的路由配置往这里加。。。
		me.add("/user", UserController.class);
		me.add("/", OSSController.class);
		me.add("/product", ProductController.class);
		me.add("/marketingController", MarketingController.class);
		me.add("/party", UserIndexController.class);
		me.add("/k2api",K2ApiController.class);

		me.add("/oauth", WeiXinOauthController.class);
		me.add("/wxorder",OrderController.class);
		me.add("/wxorder2",OrderController22.class);
		
		me.add("/jlforder",ShopOrderController.class);
		me.add("/xb",XbOrderController.class);
		LoginInterceptor.excludes.add("/marketingController/**");
		LoginInterceptor.excludes.add("/wxorder/**");
		//AuthInterceptor.exc
	}

	/**
	 * 自定义Main数据源Model映射
	 *
	 * @param arp
	 */
	@Override
	protected void mapping(HashMap<String, ActiveRecordPlugin> arps) {
		// 获取主数据源的ARP
		ActiveRecordPlugin arp = arps.get(xx.DS_MAIN);
		arp.addMapping("party_info", "id", PartyInfo.class);
		arp.addMapping("party_user_lottery_num", "id", PartyLotteryNum.class);
		arp.addMapping("party_prize_detail", "id", PartyPrizeDetail.class);
		arp.addMapping("party_prize_group", "id", PartyPrizeGroup.class);
		arp.addMapping("party_prize_summary", "id", PartyPrizeSummary.class);
		arp.addMapping("party_rule", "id", PartyRule.class);
		arp.addMapping("party_ticket", "id", PartyTicket.class);
		arp.addMapping("party_ticket_detail", "id", PartyTicketDetail.class);
		arp.addMapping("party_type", "id", PartyType.class);
		arp.addMapping("party_user", "id", PartyUser.class);
		arp.addMapping("party_user_serinfo", "id", PartyUserSerinfo.class);
		arp.addMapping("party_prize_record", "id", PartyPrizeRecord.class);
		arp.addMapping("party_get_bonus_record", "id", PartyGetBonusRecord.class);
		// 自定义业务Model映射往这里加
//		arp.addMapping("user_info", UserInfo.class);
//		arp.addMapping("user", User.class);
//		arp.addMapping("WX_ACCOUNT", WxAccount.class);
//		arp.addMapping("WX_ORDER", WxOrder.class);
//		arp.addMapping("WX_ADD_INFO", WxAddInfo.class);
//		arp.addMapping("WX_SHOP", WxShop.class);
//		arp.addMapping("WX_SHOP_ORDER", WxShopOrder.class);
		// 获取其它数据源的ARP
		// ActiveRecordPlugin xxx = arps.get("xxx");
	}
	/**
	 * 自定义插件
	 */
	@Override
	protected void plugin(Plugins plugins) {
		/** 新增自定义数据源start **/
		// ActiveRecordPlugin arp = addDataSource(plugins, "oss", JdbcUtils.MYSQL);
		// arp.addMapping("xxx", Xxx.class);
		/** 新增自定义数据源end **/
		RedisPlugin rp = new RedisPlugin("db0","127.0.0.1");
		plugins.add(rp);
		// 添加自动扫描插件

		// ...
	}

	/**
	 * 自定义表达式(主要用于级联)
	 */
	@Override
	protected void exp() {
		super.exp();
		// 区域级联查询
		exps.put("selectAreaByLv2AndPid", "select CITY_ID ID,CITY_NAME CN from d_city where  PROVINCE_ID = ?");
		exps.put("selectAreaByLv3AndPid", "select DISTRICT_ID ID,DISTRICT_NAME CN from d_district where CITY_ID = ?");
		exps.put("selectEovaMenu", "select id,parent_id pid, name, iconskip from eova_menu;ds=eova");
		// 用法，级联动态在页面改变SQL和参数
		// $xxx.eovacombo({exp : 'selectAreaByLv2AndPid,aaa,10'}).reload();
		// $xxx.eovafind({exp : 'selectAreaByLv2AndPid,aaa,10'});
		// $xxx.eovatree({exp : 'selectAreaByLv2AndPid,10'});
	}

	/**
	 * 配置全局拦截器
	 */
	@Override
	public void configInterceptor(Interceptors me) {
		super.configInterceptor(me);
		me.add(new SessionInViewInterceptor());
	}
	
	@Override
	protected void authUri() {
		super.authUri();
		HashSet<String> uris = new HashSet<String>();
		uris.add("/xxx/**");
		// auths.put(角色ID, uris);
	}
	
	/**
	 * Field授权配置
	 */
	@Override
	protected void authField() {
		// 系统角色字段授权
		super.authField();
		addAuthField("WX_ORDER.receive_name->1,2,3,4");// 解释:eova_role_code对象的lv字段 只有角色1和角色2 可见
	}
}